import java.util.Deque;
import java.util.HashMap;
import java.util.Stack;

public class 有效的括号 {
    
    public static void main(String[] args) {
        
        System.out.println(isValid("()"));


    }

    public static boolean isValid(String s) {

        if(null== s || s.isEmpty()) return false;

        int len = s.length();

        if(len % 2 != 0){

            return false;
        }

        HashMap<Character, Character> map = new HashMap<>();
        map.put(')', '(');
        map.put(']', '[');
        map.put('}', '{');


        Stack<Character> statck = new Stack<>();

        for(char c : s.toCharArray()){

            if(map.containsKey(c)){

                if(statck.isEmpty() || map.get(c) != statck.peek()){

                    return false;
                } else {

                    statck.pop();
                }
            } else {

                statck.push(c);

            }
        }

        return statck.isEmpty();

    }
}
